package com.tyqx.hashchainbiz.service.impl;

// import com.alibaba.fastjson.JSON;
import com.tyqx.hashchainbean.module.DonateCash;
import com.tyqx.hashchainbean.module.HashIndex;
import com.tyqx.hashchainbean.module.NoHashValueJson;
import com.tyqx.hashchainbiz.service.DonateCashService;
import com.tyqx.hashchaindao.mapper.DonateCashMapper;
import com.tyqx.hashchaindao.mapper.HashIndexMapper;
import com.tyqx.hashchaindao.mapper.NoHashValueJsonMapper;
import com.tyqx.hashchainutil.common.HashValueUtil;
import hashchainutil.common.EmptyUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Future;

@Service("donateCashService")
public class DonateCashServiceImpl implements DonateCashService {

    @Resource
    private HashIndexMapper hashIndexMapper;

    @Resource
    private DonateCashMapper donateCashMapper;

    @Resource
    private NoHashValueJsonMapper noHashValueJsonMapper;

    @Async
    public Future<String> donateCash(DonateCash donateCash) throws Exception {
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date createTime=sdf.parse(sdf.format(currentDate));
        donateCash.setCreateTime(createTime);
        donateCash.setUpdateTime(createTime);
        String donateCashStr= donateCash.toString();
        //HashIndex hashIndex=HashValueUtil.getHashValue(donateCashStr);
        HashIndex hashIndex=new HashIndex();
        for (int i=0;i<4;i++){
            int sleepNum=1000;
            hashIndex= HashValueUtil.getHashValue(donateCashStr);
            String hashValue=hashIndex.getHashValue();
            String txId=hashIndex.getTxId();
            Long blockNum=hashIndex.getBlockNum();
            if(EmptyUtils.isNotEmpty(hashValue)&&EmptyUtils.isNotEmpty(txId)&&EmptyUtils.isNotEmpty(blockNum)){
                for (int j=0;j<4;j++){
                    Integer flag=hashIndexMapper.insert(hashIndex);
                    if(flag>0){
                        break;
                    }else{
                        if(j==4){
                            //tianjia database
                            String hashIndexStr= hashIndex.toString();

                        }else{
                            sleepNum=sleepNum*j*5;
                            Thread.sleep(sleepNum);
                        }
                    }
                }
                donateCash.setHashValue(hashIndex.getHashValue());
                for (int j=0;j<4;j++){
                    Integer flag=donateCashMapper.addCash(donateCash);
                    if(flag>0){
                        break;
                    }else{
                        if(j==4){
                            //tianjia database
                            String donateCashStr1= donateCash.toString();
                        }else{
                            sleepNum=sleepNum*j*5;
                            Thread.sleep(sleepNum);
                        }
                    }
                }
                break;
            }else{
                if(i==3){

                    //tianjia database
                    NoHashValueJson noHashValueJson=new NoHashValueJson();
                    noHashValueJson.setRecordJson(donateCashStr);
                    noHashValueJson.setHashTableName("donate_cash");
                    noHashValueJsonMapper.insert(noHashValueJson);
                }else{
                    sleepNum=sleepNum*i*5;
                    Thread.sleep(sleepNum);
                }
            }
        }
        return new AsyncResult<String>("添加成功");
    }

    @Override
    public List<DonateCash> getDonateCashById(String donateId, String userName) {
        return donateCashMapper.getDonateCashById(donateId);
    }

    @Override
    public List<DonateCash> getDonateCashByTime(Date startTime, Date endTime, String userName) {
        return donateCashMapper.getDonateCashByTime(startTime,endTime,userName);
    }
}
